<?

/* --|   CHAT
  Software: Socially
  Version: 1.0
  Contact: http://socially.googlecode.com
  Info: http://socially.googlecode.com
  Support: http://socially.googlecode.com
  --------------------------------------------------------------------------
  Author: goker.cebeci | http://gokercebeci.com
  --------------------------------------------------------------------------
  License: Distributed under the GNU General Public License (GPL)
  http://www.gnu.org/licenses/gpl.html
  This program is distributed in the hope that it will be useful - WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE.
  ------------------------------------------------------------------------- */

class chat {

    public static function request($ID) {
        db::query(sprintf("INSERT INTO chat (senderID, recipientID, room, `date`)
            VALUES (%d,%d,'%s',NOW())
            ON DUPLICATE KEY UPDATE `status` = 'pending', `date` = NOW();",
            UID,
            $ID,
            base64_encode($ID.'-'.UID)
            ));
        return base64_encode(UID.'-'.$ID);
    }
    
    public static function accept($ID){
        db::query(sprintf("UPDATE chat SET `status` = 'chatting' WHERE 
            senderID = %d AND recipientID = %d",$ID,UID));
    }
    
    public static function reject($ID){
        db::query(sprintf("UPDATE chat SET `status` = 'rejected' WHERE 
            senderID = %d AND recipientID = %d",$ID,UID));
    }

    public static function crontab(){
        //  REQUEST TIMEOUT
        db::query("UPDATE chat SET `status` = 'timeout' WHERE
            date < (NOW()-1000) AND `status` = 'pending'");
    }

    public static function listener() {
        // GET REQUEST
        $r = db::query(sprintf("SELECT
            senderID, room
            FROM chat WHERE 
            recipientID = %d
            AND `status` = 'pending' 
            ORDER BY `date` LIMIT 1", UID), 1);
        $p = profile::get(array('ID' => $r->senderID));

        if ($r->room)
            $f = 'if(!$(\'#chat' . $r->senderID . '\').length){'
                .'tool.bubble(\'<div id="chat' . $r->senderID . '">'
                    . addcslashes(common::template('apps/profile/template/mini.html', null, $p), "',\n,\r")
                    . '<b>'.$p->name.'</b><br/>'
                    . 'sent to you a chat request<br/>'
                    .'<a onclick="chatAccept' . $r->senderID . '()">ACCEPT</a> - '
                    .'<a onclick="chatReject' . $r->senderID . '()">REJECT</a>'
                    .'</div>\',600000);'
                    .'var chatAccept' . $r->senderID . ' = function(){'
                    .'tool.post({url:"/json/chat/accept",data:"ID='
                    . $r->senderID . '",onComplete:function(){'
                    .' location.href= "/en/chat/room/' . $r->room . '";'
                    .' $(\'#chat' . $r->senderID . '\').parent().clearQueue().fadeOut();}});};'
                    .'var chatReject' . $r->senderID . ' = function(){'
                    .'tool.post({url:"/json/chat/reject",data:"ID='
                    . $r->senderID . '",onComplete:function(){'
                    .' $(\'#chat' . $r->senderID . '\').parent().clearQueue().fadeOut(); }});};};';

        return $f;
    }

    public static function search() {

    }

    public static function sitemap() {

    }    

}

?>
